require(['./config'],()=>{
    require(['template','header','bootstrap', 'footer','cookie'],(template,header)=>{
        class ShopCase{
            constructor(){
                this.init()
            }
            init(){
                let cartList = localStorage.getItem('cart')
                if(cartList){
                    this.cartList=JSON.parse(cartList)
                    $('#shopCarInfo').html(template('shopCarInfoTemplate',{cartList:this.cartList}))
                    this.cartOption()
                }else{
                    $('#cart-empty').show()
                }
            }
            cartOption(){
                   this.calTotal()
                   this.allCheckStatus()
                    this.checkChange()
                    this.countChange()
                    this.allCheckChange()
                    this.delShop()
                    this.toPay()
            }
            calTotal(){
                $('#Paymoney').html(this.cartList.reduce((money,cart)=>{
                            if(cart.check) money+=cart.price*cart.num
                            return money       
                },0).toFixed(2))
            }
            allCheckStatus(){
                $('#allCheck').prop('checked',this.cartList.every(cart=>{
                        return cart.check
                    }))
            }
            checkChange(){
                const _this=this
                $('#shopCarInfo .shopcont input:checkbox').on('change',function(){
                    let id = $(this).parents('tr').attr('data-id')
                    const isCheck=$(this).prop('checked')
                    _this.cartList= _this.cartList.map(cart=>{
                        if(cart.id==id) cart.check=isCheck
                        return cart
                    })
                    localStorage.setItem('cart',JSON.stringify(_this.cartList))
                    _this.calTotal()
                    _this.allCheckStatus()
                })
            }
            allCheckChange(){
                const _this=this
                $('#allCheck').on('change',function(){
                    const ischeck=$(this).prop('checked') 
                    
                    _this.cartList=_this.cartList.map((cart)=>{
                        if(cart.check!==ischeck) cart.check=ischeck
                          return cart
                    }) 
                    localStorage.setItem('cart',JSON.stringify(_this.cartList)) 
                    _this.calTotal() 
                    $('.checks').prop('checked',ischeck)
                })       
            }
            countChange(){
                const _this=this
                $('.shopcont').on('click','.count-btn',function(){ 
                    let id =$(this).parents('tr').attr('data-id')
                     if($(this).hasClass('minus')){
                         _this.cartList= _this.cartList.map(cart=>{
                            if(cart.id==id) {
                                cart.num--
                                if(cart.num<1) {
                                    cart.num=0 
                                    $(this).prop('disabled','disabled')}
                                $(this).next().val(Number($(this).next().val())-1) 
                                $(this).parent().next().find('span').html(($(this).next().val()*$(this).parent().prev().find('span').html()).toFixed(2))
                            }
                               
                              return cart  
                            })
                    }else if($(this).hasClass('plus')){
                        $(this).prevAll('.minus').prop('disabled','')
                        _this.cartList= _this.cartList.map(cart=>{
                            if(cart.id==id){
                                cart.num++
                                $(this).prev().val(Number($(this).prev().val())+1)
                                $(this).parent().next().find('span').html(($(this).prev().val()*$(this).parent().prev().find('span').html()).toFixed(2))
                            } 
                            return cart
                          })
                    }
                       
                        localStorage.setItem('cart',JSON.stringify(_this.cartList)) 
                        _this.calTotal()  
                        header.calCartCount ()
                })
                    
            }
            delShop(){
                const _this=this
                $('.del-btn').on('click',function(){
                    if(confirm('你确定要删除么？')){
                        let id = $(this).parents('tr').attr('data-id')
                        _this.cartList= _this.cartList.filter(cart=>{
                            return cart.id!==Number(id)
                        })
                        localStorage.setItem('cart',JSON.stringify(_this.cartList))
                        $(this).parents('tr').remove()
                       
                        if(localStorage.getItem('cart')){
                            if(JSON.parse(localStorage.getItem('cart')).length==0){
                                localStorage.removeItem('cart')
                                $('#cart-empty').show()
                                $('#shopCarInfo').hide()
                            }
                        }
                       _this.calTotal() 
                       header.calCartCount ()
                       _this. allCheckStatus()
                     }
                })
            }
            toPay(){
                $('.submit-btn').on('click',()=>{
                     const username=$.cookie('username')
                     
                     if(username){
                        const is= this.cartList.every(cart=>{
                            return cart.check!=true
                        })
                        const gift=this.cartList.every(cart=>{
                            return cart.num===0
                        })
                        if(is){
                            alert('你还没有勾选商品哦')
                            
                        }
                        if(gift){
                            alert('商品数量不能为0哦~，快添加吧')
                        } 
                        if(!is && !gift){
                            location.href='/html/payMoney.html'
                        }

                        
                   }else{
                       alert('您还没有登录哦，登录后才能付钱钱~')
                       location.replace('/html/login.html')
                   }
                })
            }
        }
        new ShopCase()
    })
})